package com.alex.sevice;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import common.beans.User;
import common.config.AppConfig;
import common.util.IPUtil;
import common.util.StringUtil;
import common.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 葛成瑞
 * @Date: 2025/6/24 14:50
 */
@Service
@Slf4j
public class StaticService {

    @Resource
    HttpServletRequest request;


    public void insStat(String searchWord) {
        if (StringUtil.isNotNull(searchWord)) {
            try {
                User user = UserUtil.getUserUnloginDeniey();
                UserActInfoDetail userActInfoDetail = new UserActInfoDetail();
                userActInfoDetail.setUid(user.getUid());
                userActInfoDetail.setGid(user.getGid());
                userActInfoDetail.setRealName(user.getRealname());
                userActInfoDetail.setPhone(user.getPhone());
                String ipAddr = IPUtil.getIpAddr(request);
                userActInfoDetail.setIp(ipAddr);
                userActInfoDetail.setCode(AppConfig.APP_ID + "");
                userActInfoDetail.setModuleName(AppConfig.APP_ID + "");
                userActInfoDetail.setSk(searchWord);
                String URL = "https://qfxcte.qfxuexi.com/statistic/add-model-use";
                String post = HttpUtil.post(URL, JSONObject.toJSONString(userActInfoDetail));
                log.info("统计结果：{}", post);
            } catch (Exception e) {
                log.error("统计异常");
            }
        }
    }

    public void insStat(String searchWord,String module) {
        if (StringUtil.isNotNull(searchWord)) {
            try {
                User user = UserUtil.getUserUnloginDeniey();
                UserActInfoDetail userActInfoDetail = new UserActInfoDetail();
                userActInfoDetail.setUid(user.getUid());
                userActInfoDetail.setGid(user.getGid());
                userActInfoDetail.setRealName(user.getRealname());
                userActInfoDetail.setPhone(user.getPhone());
                String ipAddr = IPUtil.getIpAddr(request);
                userActInfoDetail.setIp(ipAddr);
                userActInfoDetail.setCode(AppConfig.APP_ID + "");
                userActInfoDetail.setModuleName(module);
                userActInfoDetail.setSk(searchWord);
                String URL = "https://qfxcte.qfxuexi.com/statistic/add-model-use";
                String post = HttpUtil.post(URL, JSONObject.toJSONString(userActInfoDetail));
                log.info("统计结果：{}", post);
            } catch (Exception e) {
                log.error("统计异常");
            }
        }
    }

}
